<script lang="ts">
export default {
  name: 'LoginCallback'
}
</script>

<script setup lang="ts">
import { ref } from 'vue'
import CallbackBind from './components/CallbackBind.vue'
import CallbackRegister from './components/CallbackRegister.vue'
import LoginFooter from './components/LoginFooter.vue'
import LoginHeader from './components/LoginHeader.vue'
import type { QQUserInfo } from '@/types'
import { useMemberStore } from '@/store'

const isBind = ref(true)

// 枚举类型，不能写在.d.ts上面，
// 因为枚举类型，需要生成真正的js代码
// 而.d.ts，不会生成任何js的代码
enum LoginSource {
  PC = 1,
  WebApp,
  MiniProgram,
  Android,
  IOS,
  QQ,
  WeChat
}
const memberStore = useMemberStore()
const { loginQQUnionId } = memberStore
const unionId = ref('')
const userInfo = ref<QQUserInfo>()
// 检查QQ用户是否已登录
if (QC.Login.check()) {
  // 获取QQ用户唯一标识openId
  QC.Login.getMe((openId) => {
    // console.log('🗝️openId', openId)
    unionId.value = openId
    // 获取openId后，检查是否已绑定手机号，尝试直接登录
    loginQQUnionId({
      unionId: openId,
      source: LoginSource.QQ
    })
  })
  // 获取用户资料
  QC.api('get_user_info').success((res: QQUserInfo) => {
    // console.log('😀获取用户资料', res)
    userInfo.value = res
  })
}
</script>

<template>
  <!-- 头部 -->
  <LoginHeader>联合登录</LoginHeader>
  <!-- 主体 -->
  <section class="container">
    <nav class="tab">
      <a href="javascript:;" :class="{ active: isBind }" @click="isBind = true">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a href="javascript:;" :class="{ active: !isBind }" @click="isBind = false">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content">
      <KeepAlive>
        <component
          :is="isBind ? CallbackBind : CallbackRegister"
          :union-id="unionId"
          :user-info="userInfo"
        />
      </KeepAlive>
    </div>
  </section>
  <!-- 底部 -->
  <LoginFooter />
</template>

<style scoped lang="less">
.container {
  padding: 25px 0;
}

.tab {
  padding-top: 40px;
  height: 80px;
  font-size: 18px;
  text-align: center;
  background: #fff;

  a {
    display: inline-block;
    width: 350px;
    color: #666;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }

    i {
      font-size: 22px;
      vertical-align: middle;
    }

    span {
      margin-left: 4px;
      vertical-align: middle;
    }
  }
}

.tab-content {
  min-height: 400px;
  background: #fff;
}
</style>
